//
//  ExamineThreeViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/26.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "ExamineThreeViewController.h"
#import "ExamineTabBar.h"
#import "ExamineLoginViewController.h"
#import "ExamineRegisterViewController.h"
#import "UserInfo.h"
#import "UserManger.h"
#import "UIAlertView+Blocks.h"
#import "MessageViewController.h"
#import "WebShowViewController.h"
#import "FeedBackViewController.h"
#import "ShareView.h"
#import "AboutViewController.h"
#import "ExamineApplyViewController.h"

@interface ExamineThreeViewController (){
    ExamineTabBar                    *tabBarView;
    NSString                         *creditUrl;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *loginLabel;

@end

@implementation ExamineThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.title = @"我的";
    tabBarView = [ExamineTabBar sharedExamineTabBar];
    [self.navigationItem setHidesBackButton:YES];
    
    self.scrollView.alwaysBounceVertical = YES;
    self.scrollView.contentSize = CGSizeMake(0, 518);
    
    
    
    NSDictionary *configDic = [[LoadConfig shareStance] getPerson];
    if (configDic.count > 0) {
        creditUrl = [configDic objectForKey:@"credit"];
    }
    [[LoadConfig shareStance] loadPersonSuccess:^(NSDictionary *dic) {
        creditUrl = [dic objectForKey:@"credit"];
    }];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:3];
    [self refreshView];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset = scrollView.contentOffset.y;
    float height = 128;
    if (yOffset < 0) {
        CGFloat factor = ((ABS(yOffset) + height)*SCREEN_WIDTH_DEVICE)/height;
        CGRect f = CGRectMake(0, 0, factor, height+ABS(yOffset));
        self.bgImageView.frame = f;
    } else {
        if (yOffset < height) {
            CGRect f = self.bgImageView.frame;
            f.size.height = -yOffset + height;
            self.bgImageView.frame = f;
        }
    }
}

- (IBAction)loginPress {
    if ([UserManger hasUserLogged]) {
        if (IOS8_OR_LATER) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定退出登录？" preferredStyle: UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [UserManger logoutCurrentUser];
                [self refreshView];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            //弹出提示框；
            [self.navigationController presentViewController:alert animated:true completion:nil];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"是否确定退出登录？"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"退出", nil];
            alert.alertViewStyle = UIAlertViewStyleDefault;
            alert.tapBlock = ^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == alertView.firstOtherButtonIndex) {
                    [UserManger logoutCurrentUser];
                    [self refreshView];
                }
            };
            [alert show];
        }
    }else{
        ExamineLoginViewController *viewCon = [[ExamineLoginViewController alloc] initWithNibName:@"ExamineLoginViewController" bundle:nil];
        viewCon.RefreshHandle = ^(){
            [self refreshView];
        };
        CustomNavigationController *navCon = [[CustomNavigationController alloc] initWithRootViewController:viewCon];
        [navCon setNavigationBarStyle:NO];
        [self.navigationController presentViewController:navCon animated:YES completion:^{
        }];
    }
}

- (IBAction)messagePress {
    MessageViewController *viewCon = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
    viewCon.foraml = NO;
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)xinyongPress {
    if (creditUrl.length > 0) {
        WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
        viewCon.urlStr = creditUrl;
        [self.navigationController pushViewController:viewCon animated:YES];
    }else{
        [[LoadConfig shareStance] loadPersonSuccess:^(NSDictionary *dic) {
            creditUrl = [dic objectForKey:@"credit"];
            WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
            viewCon.urlStr = creditUrl;
            [self.navigationController pushViewController:viewCon animated:YES];
        }];
    }
}

- (IBAction)applyPress {
    if ([UserManger hasUserLogged]) {
        ExamineApplyViewController *viewCon = [[ExamineApplyViewController alloc] initWithNibName:@"ExamineApplyViewController" bundle:nil];
        [self.navigationController pushViewController:viewCon animated:YES];
    }else{
        [self loginPress];
    }
}

- (IBAction)changePress {
    if ([UserManger hasUserLogged]) {
        ExamineRegisterViewController *viewCon = [[ExamineRegisterViewController alloc] initWithNibName:@"ExamineRegisterViewController" bundle:nil];
        viewCon.type = RegisterType_change;
        viewCon.RefreshHandle = ^(){
            
        };
        [self.navigationController pushViewController:viewCon animated:YES];
    }else{
        [self loginPress];
    }
}

- (IBAction)feedbackPress {
    FeedBackViewController *viewCon = [[FeedBackViewController alloc] initWithNibName:@"FeedBackViewController" bundle:nil];
    viewCon.foraml = NO;
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)aboutPress {
    AboutViewController *viewCon = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
    viewCon.foraml = NO;
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)sharePress {
    ShareView *shareView = [[ShareView alloc] initItemContent:nil withUrl:nil];
    [self.view.window addSubview:shareView];
}

- (void)refreshView{
    if ([UserManger hasUserLogged]) {
        UserInfo *user = [UserManger currentLoggedInUser];
        self.loginLabel.text = user.phone;
    }else{
        self.loginLabel.text = @"登录/注册";
    }
}
@end
